/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package data;

import business.*;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.sql.SQLException;
import java.sql.ResultSet;

/**
 *
 * @author Mário
 */
public class ReclamacaoData extends InterfaceBD{

    public ReclamacaoData(){
        super();
    }

    public void addReclamacao(int codCliente, GregorianCalendar data, String descricao) throws SQLException {
        int numrecl = getnextCodRecla();
        int dia =  data.get(GregorianCalendar.DAY_OF_MONTH)+1;
        int mes = data.get(GregorianCalendar.MONTH)+1;
        int ano = data.get(GregorianCalendar.YEAR);
        String dataS = ano+"-"+mes+"-"+dia;
        String query = "Insert Into Reclamacao VALUES("+numrecl+","+codCliente+",'"+descricao+"','"+dataS+"')";
        System.out.println(query);
        res = st.executeUpdate(query);

    }

    public ArrayList<Reclamacao> daReclamacoes() throws Exception {
        ArrayList<Reclamacao> resultado = new ArrayList<Reclamacao>();
        String query = "Select * from Reclamacao";
        System.out.println(query);
        rs = st.executeQuery(query);

        boolean aux = false;
        while(rs.next()){
            aux = true;
            GregorianCalendar data = new GregorianCalendar();
            data.setTime(rs.getDate(4));
            resultado.add(new Reclamacao(rs.getInt(1),rs.getInt(2),rs.getString(3),data));
        }

        if(aux == false){

            throw new Exception("BaseDados sem Pedidos");
        }else{
            return resultado;

        }
    }

    public int getnextCodRecla() throws SQLException {

        ResultSet rs;
        int num = 0;

        try {
            rs = st.executeQuery("SELECT MAX(cod_reclamacao) FROM Reclamacao");
            rs.next();
            num = rs.getInt(1);
        }
        catch (SQLException e) {

            //
        }
        return(num+1);
    }
}
